package com.sanji.sjmallmodule.goods;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallutils.ImageUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by fhm on 2016/12/26.
 * 分类页商品展示列表
 */

public class SearchGoodsAdapter extends BaseAdapter {
    private Context context;
    private List<GoodsBean> mData;
    private RelativeLayout.LayoutParams imagParams = null;

    public SearchGoodsAdapter(Context context, List<GoodsBean> mData) {
        this.context = context;
        this.mData = mData;
        //设置图片大小
        int imageWidth = ImageUtil.setImageWidth(context, 4);
        imagParams = new RelativeLayout.LayoutParams(imageWidth, imageWidth);
//        imagParams = new RelativeLayout.LayoutParams(imageWidth, ImageUtil.setImageHightSampleWidth(imageWidth));
//        imagParams.setMargins(20, 20, 20, 20);
    }

    public void notifyAdapter(List<GoodsBean> mData) {
        this.mData = mData;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vHolder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.adapter_search_goods_list_item, null);
            vHolder = new ViewHolder(view);
            view.setTag(vHolder);
        } else {
            vHolder = (ViewHolder) view.getTag();
        }
        GoodsBean goodsBean = mData.get(i);
        int stockNumb = goodsBean.getStock();
        vHolder.searchGoodsName.setText(goodsBean.getPhoneName());
        vHolder.searchGoodsPrice.setText("￥" + goodsBean.getPhonePrice());
        vHolder.searchGoodsImg.setLayoutParams(imagParams);
        if (stockNumb > 10) {
            vHolder.stockNumberTv.setText("库存量:有货");
        } else {
            vHolder.stockNumberTv.setText("库存量:" + stockNumb + " 件");
        }
        //
        Glide.with(context).load(goodsBean.getUrl())
                //使用centerCrop是利用图片图填充ImageView设置的大小，如果ImageView的Height是match_parent则图片就会被拉伸填充
                .centerCrop()
                //当加载网络图片时，由于加载过程中图片未能及时显示，此时可能需要设置等待时的图片，通过placeHolder()方法
                .placeholder(R.mipmap.default_3j_img)
                //当加载图片失败时，通过error(Drawable drawable)方法设置加载失败后的图片显示
                .error(R.mipmap.default_3j_img)
                .crossFade()
                //DiskCacheStrategy.SOURCE：缓存原始数据，DiskCacheStrategy.RESULT：缓存变换(如缩放、裁剪等)后的资源数据，
                // DiskCacheStrategy.NONE：什么都不缓存，DiskCacheStrategy.ALL：缓存SOURC和RESULT。
                // 默认采用DiskCacheStrategy.RESULT策略，对于download only操作要使用DiskCacheStrategy.SOURCE。
//                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(vHolder.searchGoodsImg);

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.search_goods_img)
        ImageView searchGoodsImg;
        @BindView(R.id.search_goods_name)
        TextView searchGoodsName;
        @BindView(R.id.search_goods_price)
        TextView searchGoodsPrice;
        @BindView(R.id.stock_number_tv)
        TextView stockNumberTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
